package com.example.lanouhn.bantangapp.activities.utils;


import com.example.lanouhn.bantangapp.activities.contants.Search_product;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 解析Json数据
 */
public class JsonUtils_Search_product {

    /**
     * @param json json字符串
     * @return 搜索框下的数据
     */
    public static List<Search_product> getsrachList(String json) {
        List<Search_product> list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(json);

            JSONArray jsonArray = jsonObject.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject j = jsonArray.getJSONObject(i);


                Search_product m = new Search_product();
                m.setId(j.getString("id"));
                m.setName(j.getString("name"));
                m.setIcon(j.getString("icon"));
                m.setSubclassList(getList(j.getJSONArray("subclass")));
                list.add(m);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private static List<Search_product.Subclass> getList(JSONArray subarray) {

        List<Search_product.Subclass> list = new ArrayList<>();
        for (int i = 0; i < subarray.length(); i++) {

            try {
                JSONObject j = subarray.getJSONObject(i);
                Search_product.Subclass sub = new Search_product.Subclass();
                sub.setId(j.getString("id"));
                sub.setName(j.getString("name"));
                sub.setIcon(j.getString("icon"));
                list.add(sub);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return list;
    }
}